package com.systemservice.controller;

import annotation.DisableBaseResponse;
import com.systemservice.service.PremissionService;
import com.systemservice.service.RoleService;
import generator.domain.Premission;
import generator.support.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import javax.annotation.security.PermitAll;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/sys/pre")
@CrossOrigin
public class premissionController {
    @Autowired
    PremissionService premissionService;

    @Autowired
    RoleService roleService;

    /**
     * 查询所有权限信息
     */
    @GetMapping("/getPre")
    public List<Map<String, Object>> getPremission(){
        return premissionService.getPre();
    }

    /**
     * 模糊查询权限信息
     */
    @GetMapping("/query")
    public List<Map<String, Object>> queryPre(@RequestParam(value = "role", required = false) String role,
                                              @RequestParam(value = "pre", required = false) String pre){
        String tempRole = "";
        String tempPre = "";
        if (role != null){
            tempRole = role;
        }
        if (pre != null){
            tempPre = pre;
        }
        return premissionService.queryPre(tempRole, tempPre);
    }

    /**
     * 添加权限信息
     */
    @PostMapping("/addPre")
    @DisableBaseResponse
    public ResponseResult<Object> addPre(@RequestBody Premission pre){

        return premissionService.addPre(pre);
    }

    /**
     * 根据ID删除权限
     */
    @GetMapping("/delete/{id}")
    @DisableBaseResponse
    public ResponseResult<Object> removePre(@PathVariable String id){
        int temp = -1;
        try{
            temp = Integer.parseInt(id);
        } catch (NumberFormatException e){
            return new ResponseResult<>(400, "输入不合法");
        }
        if (premissionService.removeById(temp)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "删除失败");
    }

    /**
     * 修改权限信息
     */
    @PostMapping("/edit")
    @DisableBaseResponse
    public ResponseResult<Object> editPre(@RequestBody Premission premission){
        log.info("PremissionController -> editPre");
        return premissionService.editPre(premission);
    }
}
